﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.Produce.Customers;
using THE_WIND_RISES.Common.Domans.Produce.Resources;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.Application.Commands.Customerser
{
    /// <summary>
    /// 客户修改
    /// </summary>
    public class CustomerUpdateHandler : IRequestHandler<CustomerUpdateCommend, Result>
    {
        private readonly IBaseRepository<Customer> Customer;
        private readonly IMapper mapper;

        public CustomerUpdateHandler(IBaseRepository<Customer> Customer, IMapper mapper)
        {
            this.Customer = Customer;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(CustomerUpdateCommend request, CancellationToken cancellationToken)
        {
            var results = new Result() { Code = (int)CommonErrorCodes.Delete };

            var AddData = mapper.Map<Customer>(request);
            string sql = "UPDATE Customer SET CustomerNo=@CustomerNo ,CustomerName=@CustomerName ,CustomerType=@CustomerType ,CustomerPhone=@CustomerPhone " +
                         ",CustomerEmail=@CustomerEmail ,IndustryId=@IndustryId,CustomerAddress=@CustomerAddress," +
                         "CountryId =@CountryId ,CityId =@CityId,CustomerLabel =@CustomerLabel,CustomerLogotype =@CustomerLogotype,CustomerState=@CustomerState WHERE CustomerId =@CustomerId";
            var list = Customer.Update(sql, new
            {
                CustomerId = AddData.CustomerId,
                CustomerNo = AddData.CustomerNo,
                CustomerName = AddData.CustomerName,
                CustomerType = AddData.CustomerType,
                CustomerPhone = AddData.CustomerPhone,
                CustomerEmail = AddData.CustomerEmail,
                IndustryId = AddData.IndustryId,
                CustomerAddress = request.CustomerAddress,
                CountryId = request.CountryId,
                CityId = request.CityId,
                CustomerLabel = request.CustomerLabel,
                CustomerLogotype = request.CustomerLogotype,
                CustomerState = request.CustomerState,

            });
            if (list != null)
            {
                results.Code = (int)CommonErrorCodes.Success;
                results.Msg = "修改成功";
            }

            return results;
        }
    }
}
